class_name PlayerStats
extends Resource

# ===============================================

@export_range(0, 9999) var gold: int :
	set(value):
		gold = value
		emit_changed()


@export_range(0, 99) var xp: int :
	set(value):
		xp = value
		emit_changed()
		
		if level == MAX_LEVEL:
			return
		
		var xp_requirement = get_current_xp_requirement()
		while xp >= xp_requirement:
			level += 1
			if level == MAX_LEVEL:
				emit_changed()
				return
			xp -= xp_requirement
			xp_requirement = get_current_xp_requirement()
			emit_changed()


@export_range(1, MAX_LEVEL) var level: int :
	set(value):
		level = value
		emit_changed()

# ===============================================

const XP_REQUIREMENTS: Array[int] = [
#   1   2   3   4   5   6   7   8   9   10
	  2,  2,  6,  10, 14, 20, 36, 48, 84
]

const MAX_LEVEL: int = 10

const ROLL_RARITIES: Array[Array] = [
	[UnitStats.Rarity.COMMON],                                                                                                       # 1
	[UnitStats.Rarity.COMMON],                                                                                                       # 2
	[UnitStats.Rarity.COMMON, UnitStats.Rarity.UNCOMMON],                                                                            # 3
	[UnitStats.Rarity.COMMON, UnitStats.Rarity.UNCOMMON, UnitStats.Rarity.RARE],                                                     # 4
	[UnitStats.Rarity.COMMON, UnitStats.Rarity.UNCOMMON, UnitStats.Rarity.RARE, UnitStats.Rarity.EPIC],                              # 5
	[UnitStats.Rarity.COMMON, UnitStats.Rarity.UNCOMMON, UnitStats.Rarity.RARE, UnitStats.Rarity.EPIC],                              # 6
	[UnitStats.Rarity.COMMON, UnitStats.Rarity.UNCOMMON, UnitStats.Rarity.RARE, UnitStats.Rarity.EPIC, UnitStats.Rarity.LEGENDARY],  # 7
	[UnitStats.Rarity.COMMON, UnitStats.Rarity.UNCOMMON, UnitStats.Rarity.RARE, UnitStats.Rarity.EPIC, UnitStats.Rarity.LEGENDARY],  # 8
	[UnitStats.Rarity.COMMON, UnitStats.Rarity.UNCOMMON, UnitStats.Rarity.RARE, UnitStats.Rarity.EPIC, UnitStats.Rarity.LEGENDARY],  # 9
	[UnitStats.Rarity.COMMON, UnitStats.Rarity.UNCOMMON, UnitStats.Rarity.RARE, UnitStats.Rarity.EPIC, UnitStats.Rarity.LEGENDARY],  # 10
]

const ROLL_CHANCES: Array[Array] = [
	[1.00],                          # 1
	[1.00],                          # 2
	[0,75, 0.25],                    # 3
	[0.55, 0.30, 0.15],              # 4
	[0.45, 0.33, 0.20, 0.02],        # 5
	[0.30, 0.40, 0.25, 0.05],        # 6
	[0.19, 0.30, 0.35, 0.10, 0.01],  # 7
	[0.18, 0.25, 0.32, 0.22, 0.03],  # 8
	[0.10, 0.20, 0.25, 0.35, 0.10],  # 9
	[0.05, 0.10, 0.20, 0.40, 0.25],  # 10
]

# ===============================================

func get_current_xp_requirement() -> int:
	return XP_REQUIREMENTS[clampi(level, 1, MAX_LEVEL) - 1]


func get_random_rarity_for_level() -> UnitStats.Rarity:
	var rng = RandomNumberGenerator.new()
	var array: Array = ROLL_RARITIES[level - 1]
	var weights: PackedFloat32Array = PackedFloat32Array(ROLL_CHANCES[level - 1])
	return array[rng.rand_weighted(weights)]
